﻿using System.Collections.Generic;
using System;
using VoxelBlock.VoxelBlock3D;
using VoxelBlock;
using Vector3Int = VoxelBlock.Vector3Int;

public class OperationImplementScaleSelect : OperationImplement
{

    List<Vector3Int> oldSerializeVoxel = new List<Vector3Int>();
    //Dictionary<int, Vector3Int>  allSelectVoxel;
    Vector3Int rectangleOldPosition;
    Vector3Int rectangleOldPositionB;
    VoxelCategories.OperationSelectOperator selectOperator;
    //DrawVoxel3DCore nowDrawVoxel3DCore;
    Vector3Int[] zeroVector3Int = new Vector3Int[0];


    //public OperationImplementScaleSelect(ProxyOperation proxy) : base(proxy)
    //{

    //}

    public override void Start(StartArg arg)
    {
        this.selectOperator = arg.selectOperator;
        base.Start(arg);
        //this.allSelectVoxel = nowDrawVoxel3DCore.allSelectVoxel;
    }

    public override void Run(RunArg arg)
    {
        if (rectangleOldPosition != arg.nowSelectPosition)
        {
            rectangleOldPosition = arg.nowSelectPosition;

            var arrayPoint = BasicAlgorithm.GetRectanglePointArray(arg.gestureStartPosition, rectangleOldPosition);

            switch (selectOperator)
            {
                case VoxelCategories.OperationSelectOperator.Union:
                    nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 255);
                    oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 1);
                    break;
                case VoxelCategories.OperationSelectOperator.Remove:
                    nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 1);
                    oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 255);
                    break;
                default:
                    break;
            }
        }
    }

    public void NowSelect(Vector3Int positionA, Vector3Int positionB)
    {
        if (rectangleOldPosition != positionA || rectangleOldPositionB != positionB)
        {
            rectangleOldPosition = positionA;
            rectangleOldPositionB = positionB;

            var arrayPoint = BasicAlgorithm.GetRectanglePointArray(positionA, positionB);

            switch (selectOperator)
            {
                case VoxelCategories.OperationSelectOperator.Union:
                    nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 255);
                    oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 1);
                    break;
                case VoxelCategories.OperationSelectOperator.Remove:
                    nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 1);
                    oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 255);
                    break;
                default:
                    break;
            }
        }
    }
    public void Write()
    {
        if (oldSerializeVoxel.Count > 0)
        {
            switch (selectOperator)
            {
                case VoxelCategories.OperationSelectOperator.Union:
                    proxy.Write(VoxelCategories.Operation.SelectAdd, zeroVector3Int, oldSerializeVoxel);
                    oldSerializeVoxel = new List<Vector3Int>();
                    break;
                case VoxelCategories.OperationSelectOperator.Remove:
                    proxy.Write(VoxelCategories.Operation.SelectRemove, oldSerializeVoxel, zeroVector3Int);
                    oldSerializeVoxel = new List<Vector3Int>();
                    break;
                default:
                    break;
            }
        }
        TransformScaleBox.Thas.transform.root.gameObject.SetActive(false);
    }

    public override void Over(OverArg arg)
    {
        if (oldSerializeVoxel.Count > 0)
        {
            TransformScaleBox.Thas.Show(arg.gestureStartPosition, rectangleOldPosition);
            TransformScaleBox.Thas.transform.root.gameObject.SetActive(true);
            TransformScaleBox.Thas.NowSelect = NowSelect;
            TransformScaleBox.Thas.Over = Write;
        }


        

        //switch (selectOperator)
        //{
        //    case VoxelCategories.OperationSelectOperator.Union:
        //        proxy.Write(VoxelCategories.Operation.SelectAdd, zeroVector3Int, oldSerializeVoxel);
        //        oldSerializeVoxel = new List<Vector3Int>();
        //        break;
        //    case VoxelCategories.OperationSelectOperator.Remove:
        //        proxy.Write(VoxelCategories.Operation.SelectRemove, oldSerializeVoxel, zeroVector3Int);
        //        oldSerializeVoxel = new List<Vector3Int>();
        //        break;
        //    default:
        //        break;
        //}
    }

}


